package main

import (
	"fmt"
	"log"

	"gitee.com/cleango/gftenant"
	"gitee.com/cleango/gftenant/example/internal/dao"
	"gitee.com/cleango/gftenant/example/internal/model/do"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
)

func checkErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
func main() {
	gftenant.DBHandle(func(tenantId string) (gftenant.Model, error) {
		return gftenant.Model{
			DbName:    tenantId,
			DbAddr:    "localhost",
			DbPort:    "3306",
			DbAccount: "root",
			DbPass:    "123456",
		}, nil
	})
	ctx := gctx.New()
	model := do.SysTenant{
		Name:     "hello",
		Note:     "dc",
		TenantId: "",
	}
	ctx = gftenant.SetTenant(ctx, "tenant_c")

	_, err := dao.SysTenant.Ctx(ctx).Insert(&model)
	checkErr(err)
	ctx = gftenant.SetTenant(ctx, "tenant_db")
	_, err = dao.SysTenant.Ctx(ctx).Insert(&model)
	checkErr(err)
	r, err := dao.SysTenant.Ctx(ctx).All()
	checkErr(err)
	fmt.Println("B tenant", r)
	dao.SysTenant.Ctx(ctx).Where(do.SysTenant{
		Name: "hello",
	}).Update(g.Map{
		"name": "hello1",
	})
	r, err = dao.SysTenant.Ctx(ctx).All()
	checkErr(err)
	fmt.Println("B tenant", r)

	dao.SysTenant.Ctx(ctx).Where("name", "hello1").Delete()

	ctx = gftenant.SetTenant(ctx, "tenant_c")
	_, err = dao.SysTenant.DB(ctx).Exec(ctx, "insert into sys_tenant(name) select name from sys_tenant")
	checkErr(err)
	r, err = dao.SysTenant.Ctx(ctx).All()
	checkErr(err)
	dao.SysTenant.Ctx(ctx).Where("name", "hello").Delete()
	fmt.Println(r)
}
